package com.lb.function;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author zhangjun
 * @since 2025/04/21
 */
public class SupplierTest {
    Map<String, Album> cache = new HashMap<>();

    public Album getAlbum(String id, Supplier<Album> supplier) {
        return cache.computeIfAbsent(id, (k) -> supplier.get());
    }

    public <T> T saleExecute(Supplier<T> supplier, Supplier<T> callback) {
        try {
            return supplier.get();
        } catch (Exception e) {
            return callback.get();
        }
    }

    public static void main(String[] args) {
        SupplierTest supplierTest = new SupplierTest();
        Album album = supplierTest.getAlbum("1", () -> new Album("zhangjun", "test"));
        System.out.println(album);

        Album saleAlbum = supplierTest.saleExecute(() -> {
            System.out.println("saleExecute");
            return new Album("zhangjun", "test");
        }, () -> {
            System.out.println("saleExecute callback");
            return new Album("zhangjun", "test");
        });

    }

}
